package com.github.soiff.shadow.plugin.helper;

import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;
import com.github.soiff.shadow.plugin.calc.Logics;
import com.github.soiff.shadow.plugin.calc.Numbers;
import com.google.common.base.Objects;

import java.io.IOException;

/**
 * Created by Soiff on 2016/05/20.
 *
 * @author : Soiff
 */
public class IfHelper implements Helper {
    @Override
    public CharSequence apply(Object p1, Options options) throws IOException {
        String operator = options.param(0);
        Object p2 = options.param(1);
        boolean isTrue = false;
        byte var7 = -1;
        switch(operator.hashCode()) {
            case 60:
                if(operator.equals("<")) {
                    var7 = 6;
                }
                break;
            case 62:
                if(operator.equals(">")) {
                    var7 = 8;
                }
                break;
            case 1084:
                if(operator.equals("!=")) {
                    var7 = 1;
                }
                break;
            case 1216:
                if(operator.equals("&&")) {
                    var7 = 4;
                }
                break;
            case 1921:
                if(operator.equals("<=")) {
                    var7 = 7;
                }
                break;
            case 1952:
                if(operator.equals("==")) {
                    var7 = 0;
                }
                break;
            case 1983:
                if(operator.equals(">=")) {
                    var7 = 9;
                }
                break;
            case 3968:
                if(operator.equals("||")) {
                    var7 = 5;
                }
                break;
            case 33665:
                if(operator.equals("!==")) {
                    var7 = 3;
                }
                break;
            case 60573:
                if(operator.equals("===")) {
                    var7 = 2;
                }
        }

        switch(var7) {
            case 0:
                isTrue = Objects.equal(p1, p2);
                break;
            case 1:
                isTrue = !Objects.equal(p1, p2);
                break;
            case 2:
                isTrue = p1 == p2;
                break;
            case 3:
                isTrue = p1 != p2;
                break;
            case 4:
                isTrue = Logics.and(p1, p2);
                break;
            case 5:
                isTrue = Logics.or(p1, p2);
                break;
            case 6:
                isTrue = Numbers.compare(p1, p2) == -1;
                break;
            case 7:
                isTrue = Numbers.compare(p1, p2) <= 0;
                break;
            case 8:
                isTrue = Numbers.compare(p1, p2) == 1;
                break;
            case 9:
                isTrue = Numbers.compare(p1, p2) >= 0;
        }

        return isTrue?options.fn():options.inverse();
    }
}
